﻿using System;
using System.Text;

namespace JTZY.Common
{
    /// <summary>
    /// 小票格式化器
    /// </summary>
    public class NoteFormatter
    {
        private static readonly int ROW_MAX_CHAR_LEN = 32;
        private static readonly int MAX_NAME_CHAR_LEN = 20;
        private static readonly int LAST_ROW_MAX_NAME_CHAR_LEN = 16;
        private static readonly int MAX_QUANTITY_CHAR_LEN = 6;
        private static readonly int MAX_PRICE_CHAR_LEN = 6;
        private static String orderNameEmpty = repeat(" ", MAX_NAME_CHAR_LEN);

        /// <summary>
        /// 格式化菜品列表（用于58mm打印机）
        /// 注意：默认字体排版，若是字体宽度倍大后不适用
        /// 58mm打印机一行可打印32个字符 汉子按照2个字符算
        /// 分3列： 名称20字符一般用16字符4空格填充  数量6字符  单价6字符，不足用英文空格填充 名称过长换行       
        /// </summary>
        /// <param name="foodName">菜品名称</param>
        /// <param name="quantity">数量</param>
        /// <param name="price">价格</param>
        /// <returns></returns>
        public static String formatPrintOrderItem(String foodName, int quantity, Double price)
        {
            StringBuilder builder = new StringBuilder();

            byte[] itemNames = Encoding.GetEncoding("gbk").GetBytes(foodName);
            String quanityStr = quantity.ToString();
            byte[] itemQuans = Encoding.GetEncoding("gbk").GetBytes(quanityStr);
            String priceStr = roundByTwo(price);
            byte[] itemPrices = Encoding.GetEncoding("gbk").GetBytes(priceStr);

            builder.Append(foodName);
            int mod = itemNames.Length % ROW_MAX_CHAR_LEN;

            if (mod <= LAST_ROW_MAX_NAME_CHAR_LEN)
            {
                // 在同一行,留4个英文字符的空格
                builder.Append(repeat(" ", (MAX_NAME_CHAR_LEN - mod)));
                builder.Append(quanityStr).Append(repeat(" ", (MAX_QUANTITY_CHAR_LEN - itemQuans.Length)));
                builder.Append(priceStr).Append(repeat(" ", (MAX_PRICE_CHAR_LEN - itemPrices.Length)));
            }
            else
            {
                // 另起新行
                builder.Append("<BR>");
                builder.Append(orderNameEmpty);
                builder.Append(quanityStr).Append(repeat(" ", (MAX_QUANTITY_CHAR_LEN - itemQuans.Length)));
                builder.Append(priceStr).Append(repeat(" ", (MAX_PRICE_CHAR_LEN - itemPrices.Length)));
            }
            builder.Append("<BR>");
            return builder.ToString();
        }

        /// <summary>
        /// 将double格式化为指定小数位的String，不足小数位用0补全 (小数点后保留2位)
        /// </summary>
        /// <param name="v">需要格式化的数字</param>
        /// <returns></returns>
        public static String roundByTwo(double v)
        {
            return roundByScale(v, 2);
        }

        /// <summary>
        /// 将double格式化为指定小数位的String，不足小数位用0补全
        /// </summary>
        /// <param name="v">需要格式化的数字</param>
        /// <param name="scale">小数点后保留几位</param>
        /// <returns></returns>
        public static String roundByScale(double v, int scale)
        {
            return v.ToString("0.00");
        }

        /// <summary>
        /// 重复字符
        /// </summary>
        /// <param name="character">字符</param>
        /// <param name="num">重复次数</param>
        /// <returns></returns>
        public static String repeat(string character, int num)
        {
            return new string(character.ToCharArray()[0], num);
        }

    }
}
